<?php
include_once(SERVER_ROOT_PATH.'/include/config/ml_guang_config.php');
class ml_biz_guang
{
    private $enable_cache = false;
    private $cache_time = 1200;
    private $cache_block = 6;
    private $oRedis;
    private $oMcGuang;
    private $sort = '';
    private $block = 0;
    private $tj_rid = 0;
    private $param = array();
    const COLLECT_KEY_PREFFIX = 'gfg_';

    function __construct(){
        $this->oRedis = new ml_model_guang_rdsFilter();
        if($this->enable_cache)
            $this->oMcGuang = new ml_model_guang_mc();
    }
    //涮洗还原参数
    public function flush_option(){
        $this->sort = '';
        $this->block = 0;
        $this->tj_rid = 0;
        $this->param = array();
    }
    public function check($option){
        global $ML_COLOR;
        global $ML_PRICE;
        global $ML_TAG_SORT;

        $mlcol_str2id = array_flip($ML_COLOR);

        $oRedisTag = new ml_model_guang_rdsTag();


        $priceMap = ml_factory::load_standard_conf('priceMap');
        $priceMap = $priceMap[0];
        if((!empty($option['minPrice'])) || (!empty($option['maxPrice']))){
            $option['minPrice'] = (int)$option['minPrice'];
            $option['maxPrice'] = (int)$option['maxPrice'];
            $price_limit = array('min'=>$option['minPrice'],'max'=>$option['maxPrice']);
            $price = array_search($price_limit, $priceMap);
            if($price !== false)
                $this->param['price'] = $price;
        }
        if(!empty($option['sort']) && ($option['sort'] == 'new'))
            $this->sort = $option['sort'];
        if(!empty($option['ctg']))
            $this->param['ctg'] = $option['ctg'];
        if(!empty($option['tag'])){
            $tag_array = array_filter($option['tag']);
            foreach($tag_array as $tag){
                $tag_hash = ml_tool_resid::str_hash($tag);
                $tag_type = $oRedisTag->getCtgTag($tag_hash);
                if($tag_type)
                    $this->param['tag'][$tag_type] = $tag_hash;
            }
        }
        if(!empty($option['color'])){
            $this->param['color'] = $mlcol_str2id[$option['color']];
        }
        if(!empty($option['sex'])){
            $this->param['sex'] = $option['sex'];
        }
        if(!empty($option['block']))
            $this->block = (int)$option['block'];
        if(!empty($option['tj_rid']) && ml_tool_resid::is_resid($option['tj_rid']))
            $this->tj_rid = $option['tj_rid'];
    }
    public function main(){
        $data = $this->get_guang_data();
        if(!empty($this->tj_rid)){
            $data['rows'] = Tool_array::format_2d_array($data['rows'], 'rid', Tool_array::FORMAT_FIELD2ROW);
            $goods = $data['rows'][$this->tj_rid];
            if(!empty($goods)){
                unset($data['rows'][$this->tj_rid]);
            }else{
                $goods = $this->get_goods_by_tj_rid();
            }
            if($goods){
                array_unshift($data['rows'], $goods);
            }    
        }
        return $data;
    }
    public function get_guang_data(){
        if(!empty($this->param['tag']) && (!empty($this->param['tag']['guang_ctg_word'])))
            unset($this->param['ctg']);
        $comb_key = ml_tool_tag::comb_key($this->param);
        $key_array[] = $comb_key['comb_key'];
        if(!empty($this->sort)) $key_array[] = $this->sort;
        $key_array[] = $this->block;
        $mc_key = self::COLLECT_KEY_PREFFIX.implode('_', $key_array);
        if($this->enable_cache && ($this->cache_block>=$this->block))
        {
            $data = $this->oMcGuang->get_data($mc_key);
            //var_dump($data);var_dump($mc_key);exit;
            //Tool_logger::monitorLog('guang_rdshad', 'mc | '.$mc_key, Tool_logger::LOG_LEVEL_NOTICE );
            if(is_array($data))
                return $data;
        }
        
        $rs = $this->oRedis->get_filter_result($this->param, $this->block, $this->sort);
        if(!$rs) 
            return false;
        $redisRs = $this->oRedis->get_data();
        $rid_array = $redisRs['rids'];
        $gResult = array(
            'total' => ($redisRs['total']>ML_GUANG_TOTAL ? ML_GUANG_TOTAL : $redisRs['total']),
            'rows' => array()
        );        
        if(count($rid_array)>0){
            $oContent = new ml_model_dbContent();
            $rows = $oContent->get_content_by_rids($rid_array, true);
            $goods_array = array();
            foreach($rid_array as $key=>$rid){
                $goods = $rows[$rid];
                if(!empty($goods))
                    $goods_array[$key] = $goods;
            }
            $gResult['rows'] = $goods_array;
        }
        //存到mc
        if($this->enable_cache && ($this->cache_block>=$this->block)){
            $mc_rs = $this->oMcGuang->set_data($mc_key , $gResult, $this->cache_time);
            //var_dump($mc_key);var_dump($gResult);var_dump($mc_rs);exit;
        }
        return $gResult;    
    }
    public function get_goods_by_tj_rid(){
        $oContent=new ml_model_dbContent();
        $rs=$oContent->get_by_rid($this->tj_rid);
        if($rs){
            $goods = $oContent->get_data();
            return $goods;
        }
        return false;
    }
}
?>